//侧边栏 前4项的页面跳转
var li_list = document.querySelectorAll(".select li")
li_list[0].addEventListener('click',function(){
    location.href = "/chapter12"
})
li_list[1].addEventListener('click',function(){
    location.href = "/chapter12/upload"
})
li_list[2].addEventListener('click',function(){
    location.href = "/chapter12/search"
})
li_list[3].addEventListener('click',function(){
    location.href = "/chapter12/mynode"
})

//获取用户名展示欢迎标语
userCookie = getCookie("username")
if(userCookie){
    document.querySelector(".l_img p").innerHTML= "欢迎!" + " " +userCookie
}
//得到登录用户笔记
getNodeByCookie(userCookie)
//得到登录用户的笔记方法
function getNodeByCookie(username){
    $.ajax({
        url:"node/selectMyNode",
        type:"post",
        data:JSON.stringify({
            username:username
        }),
        contentType:"application/json;charset=UTF-8",
        dataType:"json",
        success:function (data){
            console.log(data)
            //渲染到页面上
            const result = document.querySelector(".result")
            let tips = document.querySelector(".tips")
            let struct = ""
            if(data.length == 0){ //若没找到笔记或者没有笔记
                tips.style.display = "block"
            }else{
                for (var i=0;i<data.length;i++){
                    struct += '<li>\n' +
                        '                    <div class="nodename">' + data[i].nodename + '</div>\n' +
                        '                    <div class="class">' + data[i].class_name + '</div>\n' +
                        '                    <div class="language">' + data[i].language + '</div>\n' +
                        '                    <div class="alter" style="line-height: 50px;" data-index='+ i +'></div>\n' +
                        '                    <div class="delete" style="line-height: 50px;" data-index='+ i +'></div>\n' +
                        '                </li>'
                }
                result.innerHTML = struct
                //为弹窗编辑事件，且为所有的笔记添加编辑点击事件
                button_click(data)
            }

        }
    })
}
//为弹窗编辑事件，且为所有的笔记添加编辑点击事件
function button_click(data){
    //获取所有的删除和编辑按钮
    let alters = document.querySelectorAll(".alter")
    let deletes = document.querySelectorAll(".delete")
    //获取窗口
    let alter_window = document.querySelector(".alter_window")
    let delete_window = document.querySelector(".delete_window")
    //获取YES和NO按钮
    let yes = document.querySelector(".yes")
    let no = document.querySelector(".no")
    //获取表单
    let name = document.querySelector("input[type='text']")
    let class_name = document.querySelector("select[name='class']")
    let language = document.querySelector("select[name='language']")
    //获取提交按钮
    let submit = document.querySelector("input[type='submit']")
    //获取关闭按钮
    let close = document.querySelectorAll(".close")
    let other_display = false //其他窗口是否显示
    //若点击关闭，将other_display改为false，display改为none
    for(let x=0;x<close.length;x++){
        close[x].addEventListener('click',function (){
            alter_window.style.display = "none"
            delete_window.style.display = "none"
            other_display = false
        })
    }
    no.addEventListener('click',function (){
        delete_window.style.display = "none"
        other_display = false
    })
    //为所有的笔记添加编辑点击事件
    for(let i=0;i<alters.length;i++){
        alters[i].addEventListener('click',function (){
            console.log("点击了第" + this.dataset.index + "个编辑按钮")
            if (other_display) delete_window.style.display = "none"
            alter_window.style.display = "block" //窗口显示
            other_display = true
            //在窗口的表单显示信息
            name.value = data[this.dataset.index].nodename
            class_name.value = data[this.dataset.index].class_name
            language.value = data[this.dataset.index].language
            //准备检查数据
            let name_check = data[this.dataset.index].nodename
            let class_name_check = data[this.dataset.index].class_name
            let language_check = data[this.dataset.index].language
            //准请求数据：笔记id
            let nodeId = data[this.dataset.index].node_id
            let userId = data[this.dataset.index].user_id
            //点击submit，发送请求
            submit.addEventListener('click',function (){
                //数据检查
                if (name.value == name_check&&class_name.value == class_name_check&&language.value == language_check){
                    alert("请修改笔记的相关信息!")
                    return
                }
                //发送请求
                $.ajax({
                    url:"node/alter",
                    type:"post",
                    data:JSON.stringify({
                        nodename:name.value,
                        class_name:class_name.value,
                        language:language.value,
                        node_id:nodeId,
                        user_id:userId
                    }),
                    contentType:"application/json;charset=UTF-8",
                    dataType:"json",
                    success:function (data){
                        if (data){
                            alter_window.style.display = "none"
                            other_display = false
                            alert("编辑成功!")
                            location.reload()//刷新页面
                        }
                    }
                })
            })
        })
    }
    for(let j=0;j<deletes.length;j++){
        deletes[j].addEventListener('click',function (){
            if(other_display) alter_window.style.display = "none"
            delete_window.style.display = "block" //窗口显示
            other_display = true
            let node_id = data[this.dataset.index].node_id //删除笔记的id
            let user_id = data[this.dataset.index].user_id //删除笔记的作者id
            let nodename = data[this.dataset.index].nodename //删除笔记的笔记名
            let class_name = data[this.dataset.index].class_name //删除笔记的类型
            let language = data[this.dataset.index].language //删除笔记的语言
            //点击确定，发送请求
            yes.addEventListener('click',function (){
                console.log("发送请求!删除的笔记id："+node_id)
                $.ajax({
                    url:"node/deleteMyNode",
                    type: "post",
                    data:JSON.stringify({
                        node_id:node_id,
                        user_id:user_id,
                        nodename:nodename,
                        class_name:class_name,
                        language:language
                    }),
                    contentType:"application/json;charset=UTF-8",
                    dataType:"json",
                    success:function (data){
                        if(data){
                            delete_window.style.display = "none"
                            other_display = false
                            alert("删除成功!")
                            location.reload()//刷新页面
                        }else{
                            delete_window.style.display = "none"
                            other_display = false
                            alert("删除失败!")
                        }
                    }
                })
            })

        })
    }
}
//滚动事件，使弹窗永远在页面中间
let alter_window = document.querySelector(".alter_window")
let delete_window = document.querySelector(".delete_window")
let height = alter_window.offsetTop
document.addEventListener('scroll',function(){
    var scrollTop = document.documentElement.scrollTop
    if(scrollTop>=height/10){
        alter_window.style.position="fixed"
        alter_window.style.top='50%'
        delete_window.style.position="fixed"
        delete_window.style.top='50%'
    } else{
        alter_window.style.position="absolute"
        alter_window.style.top='50%'
        delete_window.style.position="absolute"
        delete_window.style.top='50%'
    }
})